package ch3.tskeexecutor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * spring对多线的支持
 * taskExecutoe任务执行器
 * ThreadPoolExector基于线程池的任务执行器
 */
@Configuration
@ComponentScan("ch3.tskeexecutor")
@EnableAsync//开启异步事务
public class TaskExecutorConfig implements AsyncConfigurer {
    //配置类实现asyncConfig接口并重写感叹AsyncExecutor 并返回一个线程池
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();//创建线程池
        taskExecutor.setMaxPoolSize(10);//最大线程数
        taskExecutor.setCorePoolSize(5);
        taskExecutor.setQueueCapacity(25);
        taskExecutor.initialize();
        return taskExecutor;

    }

    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}
